<template>
  <div class="reportFormPage">
    <div class="report-dom">
      <el-row>
        <el-col :span="5" :offset="19">
          <div class="pageNumBox">
            <span>第</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
            <span>页，共</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
            <span>页</span>
          </div>
        </el-col>
      </el-row>
      <div class="topTwoBox">
        <div class="centerName">土击实试验检测报告</div>
        <div class="codeText">
          <tr>BGLQ09003F</tr>
        </div>
      </div>
      <div class="threeTopBox">
        <div class="left-border">
          <div class="box-title">检测单位名称：</div>
          <div class>{{ form.jiancdwmc }}</div>
        </div>
        <div class="right-border">
          <span>报告编号：</span>
          <span class="placeholder">{{ form.baogbh }}</span>
        </div>
      </div>
      <div class="table-border">
        <!-- .........................1111111111111111111111111111111......................................................... -->
        <table height="210px" class="top-table">
          <tbody>
            <tr>
              <td height="40" align="center" width="15%">
                <span style="width:90px;display:inline-block">施工/委托单位</span>
              </td>
              <td width="35%" bgcolor="#ffffff">
                <span>{{ form.shigwtdw }}</span>
              </td>
              <td align="center" width="15%">
                <span style="width:90px;display:inline-block">工程名称</span>
              </td>
              <td bgcolor="#ffffff" width="35%">
                <span>{{ form.gongcmc }}</span>
              </td>
            </tr>
            <tr>
              <td height="30" align="center" width="15%">
                <span style="width:90px;display:inline-block">工程部位/用途</span>
              </td>
              <td colspan="3">
                <span>{{ form.gongcbwyt }}</span>
              </td>
            </tr>
            <tr>
              <td height="50" align="center">样品信息</td>
              <td class="specimen-border" colspan="3">
                <span>样品名称：</span>
                <span :class="{ 'placeholder': !form.yangpmc }">{{ form.yangpmc }}</span>
                <span>；样品编号：</span>
                <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>

                <span>；样品数量：</span>
                <span :class="{ 'placeholder': !form.yangpsl }">{{ form.yangpsl }}</span>
                <span>；样品状态：</span>
                <span :class="{ 'placeholder': !form.yangpzt }">{{ form.yangpzt }}</span>
                <span>；来样时间：</span>
                <span :class="{ 'placeholder': !form.yangpsj }">{{ form.yangpsj }}</span>
              </td>
            </tr>
            <tr>
              <td height="50" align="center" width="15%">
                <span style="width: 90px; display: inline-block">检测依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.jiancyj }}</span>
                <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark"
                  @dblclick.native="showBsJiancyjView" readonly />
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">判定依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.pandyj }}</span>
                <el-input v-else type="textarea" v-model="form.pandyj" :rows="2" @dblclick.native="showBsJudgeBaseView"
                  @focus="handleFocus('pandyj')" class="mark" />
              </td>
            </tr>
            <tr class="bottom-border">
              <td height="50" align="center">
                <div>主要仪器设备</div>
                <div>名称及编号</div>
              </td>
              <td colspan="3">
                <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh" class="height100 width100 mark"
                  @dblclick.native="showBsInstrumentView" readonly></el-input>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .................................22222222222222222222222222222222............................................... -->
        <table height="40px" class="middle-table">
          <tr>
            <td height="30" align="center" width="15%">
              <span style="width:90px;display:inline-block">取样地点</span>
            </td>
            <td width="35%" bgcolor="#ffffff">
              <el-input type="text" v-model="form.quywz" @focus="handleFocus('quywz')"
                @keydown.native="handleKeyDown($event, 0, 0)" :ref="'0_0'" />
            </td>
            <td height="25" align="center" width="15%">
              <span style="width:90px;display:inline-block">代表数量</span>
            </td>
            <td width="35%" bgcolor="#ffffff">
              <el-input type="text" v-model="form.daibsl" @focus="handleFocus('daibsl')"
                @keydown.native="handleKeyDown($event, 0, 1)" :ref="'0_1'" />
            </td>
          </tr>
        </table>
        <table height="280px" class="middle-table">
          <tr trindex="1">
            <td colspan="3" rowspan="2" height="60" class="td1">试验组数</td>
            <td colspan="5" rowspan="1" class="td2">试验次数</td>
            <td colspan="2" rowspan="2" class="td2" height="30">最佳含水率（%）</td>
            <td colspan="2" rowspan="2" class="td2" height="30">最大干密度（g/cm3）</td>
          </tr>
          <tr trindex="1">
            <td colspan="1" class="td1">1</td>
            <td colspan="1" class="td1">2</td>
            <td colspan="1" class="td1">3</td>
            <td colspan="1" class="td1">4</td>
            <td colspan="1" class="td1">5</td>
          </tr>
          <tr trindex="3">
            <td rowspan="2" class="td1">1</td>
            <td colspan="2" height="35" class="td2">含水率（%）</td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[0].hanslpjz" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[1].hanslpjz" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[2].hanslpjz" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[3].hanslpjz" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[4].hanslpjz" disabled />
            </td>
            <td colspan="2" rowspan="2" class="td2">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.zuijhsl1" disabled />
            </td>
            <td colspan="2" rowspan="2" class="td2">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.zuidgmd1" disabled />
            </td>
          </tr>
          <tr trindex="4">
            <td colspan="2" height="35" class="td2">干密度（g/cm3）</td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[0].ganmd" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[1].ganmd" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[2].ganmd" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[3].ganmd" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[4].ganmd" disabled />
            </td>
          </tr>
          <tr trindex="5">
            <td rowspan="2" class="td1">2</td>
            <td colspan="2" height="35" class="td2">含水率（%）</td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[5].hanslpjz" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[6].hanslpjz" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[7].hanslpjz" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[8].hanslpjz" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[9].hanslpjz" disabled />
            </td>
            <td colspan="2" rowspan="2" class="td2">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.zuijhsl2" disabled />
            </td>
            <td colspan="2" rowspan="2" class="td2">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.zuidgmd2" disabled />
            </td>
          </tr>
          <tr trindex="6">
            <td colspan="2" height="35" class="td2">干密度（g/cm3）</td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[5].ganmd" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[6].ganmd" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[7].ganmd" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[8].ganmd" disabled />
            </td>
            <td colspan="1" class="td1">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[9].ganmd" disabled />
            </td>
          </tr>
          <tr trindex="7">
            <td colspan="4" class="td4" height="40px">平均最佳含水率（%）</td>
            <td colspan="2" class="td2">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.zuijhsljz" disabled />
            </td>
            <td colspan="4" class="td4">平均最大干密度（g/cm3）</td>
            <td colspan="2" class="td2">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.zuidgmdjz" disabled />
            </td>
          </tr>
          <tr trindex="7">
            <td colspan="4" class="td4" height="40px">修正后平均最佳含水率（%）</td>
            <td colspan="2" class="td2">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[0].xiuzhpjzjhsl" />
            </td>
            <td colspan="4" class="td4">修正后平均最大干密度（g/cm3）</td>
            <td colspan="2" class="td2">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baDetailVos[0].xiuzhpjzdgmd" />
            </td>
          </tr>
          <tr trindex="8">
            <td colspan="12" class="echarts-border">
              <table-line-log-more-chart :className="'echarts-border'" :chartData="chartData" :xMin="chartData.xMin"
                :xMax="chartData.xMax" :yMin="chartData.yMin" :yMax="chartData.yMax" :interval="interval"
                :isSmooth="true" :minorSplitLine="true"></table-line-log-more-chart>
            </td>
          </tr>
        </table>
        <!-- .....................................................33333333333333333................................................... -->
        <table height="120px" class="bottom-table">
          <tr>
            <td colspan="14" height="60px" style="border-bottom-width: 1px;">
              <span v-if="isPdf" class="textarea-span">{{ form.jiancjl }}</span>
              <el-input v-else type="textarea" :rows="2" v-model="form.jiancjl" @focus="handleFocus('jiancjl')"
                @dblclick.native="getJcjl()" />
            </td>
          </tr>
          <tr>
            <td>
              <span v-if="isPdf" class="textarea-span">{{ form.fujsm }}</span>
              <el-input v-else type="textarea" :rows="2" v-model="form.fujsm" @focus="handleFocus('fujsm')" />
            </td>
          </tr>
        </table>
      </div>
      <!-- .................................44444444444............................................. -->
      <table height="22px" class="transparent">
        <tbody>
          <tr>
            <td width="10%" align="right">检测：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">审核：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">批准：</td>
            <td align="left" width="15%"></td>
            <td width="10%">日期：</td>
            <td align="left" width="15%">
              <div style="display: inline-block; margin-left: 20px">年</div>
              <div style="display: inline-block; margin-left: 20px">月</div>
              <div style="display: inline-block ;margin-left: 20px">日</div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
    <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
    <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
  </div>
</template>

<script>
import { handleFocus, emptyConvert, uniqueStr } from "@/views/reportForms/utils";
import { getTgTjssybgBsInfo, computeTgTjssybgBsInfo, queryJcjl } from "@/api/report_tg/tgTjssybgBsInfo";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import TableLineLogMoreChart from "@/views/reportForms/components/TableLineLogMoreChart.vue";

import decimal from "@/utils/big-decimal";
import Bus from "@/utils/bus";
export default {
  name: "tjssybg",
  components: {
    BsJudgeBaseView,
    BsInstrumentView,
    BsJiancyjView,
    TableLineLogMoreChart
  },
  props: {
    excelId: {
      type: [String, Number],
      default: () => ""
    },
    syType: {
      type: [String, Number],
      default: () => ""
    },
    page: {
      type: Number,
      default: () => 0
    },
    isPdf: {
      type: Boolean,
      default: () => false
    },
    printEmpty: {
      type: Boolean,
      default: () => false
    }
  },
  data () {
    return {
      // 表单参数
      form: {
        baDetailVos: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
      },
      chartData: {
        xName: "含水率（%）",
        yName: "干密度（g/ cm3）",
        xData: [],
        yData: [],
        seriesData0: [],
        seriesData1: [],
        seriesData: [],
        time: "",
        xMin: 10,
        xMax: 0,
        yMin: 10,
        yMax: 0
      },
      interval: 1
    };
  },
  watch: {
    isPdf (newVal, oldVal) {
      this.form = { ...emptyConvert(this.form, newVal) };
    }
  },
  created () {
    this.getInfo();
  },
  mounted () {
    // 检测依据
    Bus.$on("setTgBgJiancyj", (data) => {
      const { jiancyj, jiancyjIds } = data;
      const str = jiancyj + (this.form.jiancyj ? "、" + this.form.jiancyj : "");
      const ids = jiancyjIds + (this.form.jiancyjIds ? "," + this.form.jiancyjIds : "");
      this.form.jiancyj = uniqueStr(str, "、");
      this.form.jiancyjIds = uniqueStr(ids, ",");
    });
    // 主要设备
    Bus.$on("setTgBgZhuyyqsbmcjbh", (data) => {
      const { zhuyyqsbmcjbh, shebIds } = data;
      const str = zhuyyqsbmcjbh + (this.form.zhuyyqsbmcjbh ? "、" + this.form.zhuyyqsbmcjbh : "");
      const ids = shebIds + (this.form.shebIds ? "," + this.form.shebIds : "");
      this.form.zhuyyqsbmcjbh = uniqueStr(str, "、");
      this.form.shebIds = uniqueStr(ids, ",");
    });
    // 土击实试验数据
    Bus.$on("setTjssyBgTestData", (info) => {
      if (!info) {
        return;
      }
      const data = info.data;
      const count = info.page == 0 ? 0 : 5;
      data.forEach((item, index) => {
        this.form.baDetailVos[index + count].hanslpjz = item.hanslpjz;
        this.form.baDetailVos[index + count].ganmd = item.ganmd;
      });
    });
    // 最佳含水率
    Bus.$on("setTjssyBgzuijhs", (info) => {
      if (!info) {
        return;
      }
      const page = info.page + 1;
      this.form["zuijhsl" + page] = info.zuijhsl;
      this.form["zuidgmd" + page] = info.zuidgmd;
      this.form.chartData = info.chartData;
      // 计算均值
      this.calculateJz(info.page);
    });
  },
  beforeDestroy () {
    // 停止监听事件
    Bus.$off("setTgBgJiancyj");
    Bus.$off("setTgBgZhuyyqsbmcjbh");
    Bus.$off("setTjssyBgTestData");
    Bus.$off("setTjssyBgzuijhs");
  },
  methods: {
    // 获取详情
    getInfo () {
      getTgTjssybgBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
        this.form = response.data;
        if (!this.form.jiancjl) {
          this.form.jiancjl = "检测结论：";
        }
        if (!this.form.fujsm) {
          this.form.fujsm = "附加声明：";
        }
        this.setEcharts();
      });
    },
    // 显示检测依据列表
    showBsJiancyjView () {
      this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
    },
    // 检测依据选择
    selectJiancyj (info) {
      this.form.jiancyj = info.label;
      this.form.jiancyjIds = info.ids;
    },
    // 显示判定依据列表
    showBsJudgeBaseView () {
      this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
    },
    // 判断依据选择
    selectJudge (info) {
      if (!info || !info.label) {
        return;
      }
      if (!this.form.pandyj) {
        this.form.pandyj = info.label;
      } else {
        if (info && info.label) {
          this.form.pandyj += "、" + info.label;
        }
      }
      // 添加指标 info.list
    },
    // 显示仪器列表
    showBsInstrumentView () {
      this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
    },
    // 仪器选择
    selectInstrument (info) {
      this.form.zhuyyqsbmcjbh = info.label;
      this.form.shebIds = info.ids;
    },
    // 获取检测结论
    getJcjl () {
      const form = JSON.parse(JSON.stringify(this.form));
      queryJcjl(emptyConvert(form)).then((res) => {
        const info = res.data;
        this.form.jiancjl = info.jiancjl;
      });
    },
    // 计算均值
    calculateJz () {
      if (this.form.zuijhsl1 && this.form.zuijhsl2) {
        this.form.zuijhsljz = decimal.divide(decimal.add(this.form.zuijhsl1, this.form.zuijhsl2), 1);
      }
      if (this.form.zuidgmd1 && this.form.zuidgmd2) {
        this.form.zuidgmdjz = decimal.divide(decimal.add(this.form.zuidgmd1, this.form.zuidgmd2), 2);
      }
      this.calculateInfo();
    },
    // 计算图表轨迹与最大干密度等
    calculateInfo () {
      computeTgTjssybgBsInfo(this.form).then((response) => {
        this.form = response.data;
        this.setEcharts();
      });
    },
    // 拼接图表数据
    setEcharts () {
      if (!this.form.chartDatas || this.form.chartDatas.length <= 0) {
        this.chartData.seriesData = [];
        return;
      }
      let xMin = 0;
      let xMax = 0;
      let yMin = 0;
      let yMax = 0;
      this.form.chartDatas.forEach((chartData, page) => {
        let data1 = [];
        let data2 = [];
        if (!chartData || chartData.length <= 0) {
          return;
        };
        chartData.forEach((item, index) => {
          if (page == 0 && index == 0) {
            xMin = item.point[0];
            xMax = item.point[0];
            yMin = item.point[1];
            yMax = item.point[1];
          }
          if (item.point[0] < xMin) {
            xMin = item.point[0];
          }
          if (item.point[1] < yMin) {
            yMin = item.point[1];
          }
          if (item.point[0] > xMax) {
            xMax = item.point[0];
          }
          if (item.point[1] > yMax) {
            yMax = item.point[1];
          }
          switch (item.flag) {
            case "100":
              data1.push(item.point);
              break;
            case "010":
              data2.push(item.point);
              break;
          }
        });
        xMin = xMin > this.interval ? decimal.subtract(xMin, this.interval, 0) : xMin;
        yMin = yMin > 0.2 ? decimal.subtract(yMin, 0.2, 1) : yMin;
        xMax = decimal.add(xMax, this.interval, 0);
        yMax = decimal.add(yMax, 0.2, 1);
        this.chartData.xMin = xMin;
        this.chartData.yMin = yMin;
        this.chartData.xMax = xMax;
        this.chartData.yMax = yMax;
        const chart1 = {
          data: data1,
          connectNulls: true,
          smooth: true,
          symbol: "none",
          type: "line"
        };
        const chart2 = {
          type: "scatter",
          symbolSize: 4,
          data: data2
        };
        this.chartData["seriesData" + page] = [chart1, chart2];
      });
      let markLineData = [];
      const point = [Number(this.form.zuijhsljz), Number(this.form.zuidgmdjz)];
      markLineData.push([
        {
          coord: [xMin, this.form.zuidgmdjz]
        },
        {
          coord: point
        }
      ]);
      markLineData.push([
        {
          coord: [this.form.zuijhsljz, yMin]
        },
        {
          coord: point
        }
      ]);
      const chart3 = {
        data: [
          {
            value: point,
            label: { show: true, formatter: JSON.stringify(point), position: "top" },
            itemStyle: {}
          }
        ],
        connectNulls: true,
        smooth: true,
        type: "line",
        markLine: {
          symbolSize: [1],
          label: { show: false },
          silent: true,
          data: markLineData
        }
      };
      this.chartData.seriesData = [...this.chartData.seriesData0, ...this.chartData.seriesData1, chart3];
    },
    submitForm () {
      return this.form;
    },
    handleFocus (event) {
      handleFocus(event, this.page, this.form[event]);
    }
  }
};
</script>

<style scoped>
.middele-td {
  width: calc((100% - 15%) / 7);
}

.td1 {
  width: calc(100% / 14 * 1);
}

.td2 {
  width: calc(100% / 14 * 2);
}

.td3 {
  width: calc(100% / 14 * 3);
}

.td4 {
  width: calc(100% / 14 * 4);
}

.td5 {
  width: calc(100% / 14 * 5);
}

.echarts-border {
  width: calc(210mm - 40mm - 8px);
  height: 240px;
  padding: 2px !important;
  background-color: #ffffff !important;
}
</style>
